{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f3e3c1af",
   "metadata": {},
   "source": [
    "# 购物车程序\n",
    "1.用户输入使用input，input的使用，结果使用是一个字符串\n",
    "2.循环遍历商品列表，将商品信息按照一定格式输出\n",
    "3.一定格式：商品编号，商品名称，商品价格\n",
    "4.range会根据商品列表的长度变化而变化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "29844b84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额2000\n",
      "------ 请选择编号 欢迎选购 想结束购物输入q就可以离开啦 ------\n",
      "商品编号: 0 , 商品名称: python课本 , 商品价格: 60\n",
      "商品编号: 1 , 商品名称: 卫衣 , 商品价格: 200\n",
      "商品编号: 2 , 商品名称: 小米手环 , 商品价格: 199\n",
      "商品编号: 3 , 商品名称: 阿玛尼粉底液 , 商品价格: 600\n",
      "请正确输入购买商品的编号: 3\n",
      "【阿玛尼粉底液】已加入您的购物车，您的【账户余额】还有: 1400 元\n",
      "------ 请选择编号 欢迎选购 想结束购物输入q就可以离开啦 ------\n",
      "商品编号: 0 , 商品名称: python课本 , 商品价格: 60\n",
      "商品编号: 1 , 商品名称: 卫衣 , 商品价格: 200\n",
      "商品编号: 2 , 商品名称: 小米手环 , 商品价格: 199\n",
      "商品编号: 3 , 商品名称: 阿玛尼粉底液 , 商品价格: 600\n",
      "请正确输入购买商品的编号: 1\n",
      "【卫衣】已加入您的购物车，您的【账户余额】还有: 1200 元\n",
      "------ 请选择编号 欢迎选购 想结束购物输入q就可以离开啦 ------\n",
      "商品编号: 0 , 商品名称: python课本 , 商品价格: 60\n",
      "商品编号: 1 , 商品名称: 卫衣 , 商品价格: 200\n",
      "商品编号: 2 , 商品名称: 小米手环 , 商品价格: 199\n",
      "商品编号: 3 , 商品名称: 阿玛尼粉底液 , 商品价格: 600\n",
      "请正确输入购买商品的编号: q\n",
      "用户购物车:  [['阿玛尼粉底液', 600], ['卫衣', 200]]\n",
      "您的余额还有： 1200\n"
     ]
    }
   ],
   "source": [
    "goods_list =[\n",
    "    [\"python课本\",60],        # [商品名称，商品价格]\n",
    "    [\"卫衣\",200],\n",
    "    [\"小米手环\",199],\n",
    "    [\"阿玛尼粉底液\",600] \n",
    "]                            # 嵌套模式写内容\n",
    "user_shopping_car = [\n",
    "    \n",
    "]\n",
    "\n",
    "# 用户输入 使用input , input的使用，结果是一个字符串\n",
    "Alipay = int(input(\"请输入您的余额\"))\n",
    "\n",
    "\n",
    "while True:\n",
    "    # 循环遍历商品列表，将商品信息按照一定格式输出\n",
    "    # 一定格式： 商品编号：xx , 商品名称： xx,商品价格: xx\n",
    "    print(\"------ 请选择编号 欢迎选购 想结束购物输入q就可以离开啦 ------\")\n",
    "    for i in range(len(goods_list)):   # range会根据商品列表的长度变化而变化\n",
    "        print(\"商品编号: %s , 商品名称: %s , 商品价格: %s\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "    # 让用户输入商品编号进行商品的购买。\n",
    "    # 1. 用户输入的内容是否符合规范？（用户买的东西有没有）\n",
    "\n",
    "    # 思考：用户除了输入数值外，有没有可能输入的不是数值\n",
    "\n",
    "\n",
    "    choice = input(\"请正确输入购买商品的编号: \")\n",
    "    if choice.isdigit():  # isdigit 判断字符串的内容是否是数值\n",
    "        choice = int(choice)\n",
    "        if choice > len(goods_list):\n",
    "        #   （2）如果没有该商品，返回没有\n",
    "           print(\"您选择的商品不存在\")\n",
    "        else:\n",
    "           #  （1）如果有，判断金额是否够：用户金额 Alipay 商品的金额 goods_list[choice][1]\n",
    "            if Alipay > goods_list[choice][1]:\n",
    "               # 用户的金额够：1. 增加该商品到用户购物车 2. 扣除余额\n",
    "                user_shopping_car.append(goods_list[choice])\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                print(\"【%s】已加入您的购物车，您的【账户余额】还有: %s 元\"%(goods_list[choice][0],Alipay))\n",
    "            else:\n",
    "                print(\"您的余额不足，请更换支付方式\")\n",
    "    elif choice == \"q\":\n",
    "        # 用户退出购买\n",
    "        print(\"用户购物车: \",user_shopping_car)\n",
    "        print(\"您的余额还有：\",Alipay)\n",
    "        break\n",
    "    else:\n",
    "        print(\"请输入商品编号！！！\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3efc43da",
   "metadata": {},
   "source": [
    "元组\n",
    "1.它是和list几乎完全一样的序列数据\n",
    "2.它与list不同的地方是，不可变（因为不可变，所以几乎没有方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9d7f45a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = \"chensimin\",\"hyuna\",\"aom\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "23a7d948",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('chensimin', 'hyuna', 'aom')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5081fb16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d7041397",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a982e567",
   "metadata": {},
   "source": [
    "# 字典(dict)\n",
    "一、字典的创建方式;\n",
    "1.直接创建字典，按照字典的结构：{key1:value1 , key2:value2 , ......}\n",
    "2.dict()创建字典，dict(key1=value1，key2:value2， ...)\n",
    "3.新建空字典，然后增加值的方式创建字典\n",
    "二、字典的性质"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b65baa16",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "95b38412",
   "metadata": {},
   "source": [
    "课后作业\n",
    "字典运用\n",
    "字典练习\n",
    "请尝试使用和学习字典的方法（必须完成）\n",
    "给一组数据，利用字典查询的方式尝试查找指定的数据（尝试完成）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6461c32a",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info = {\n",
    "    \"stu10001\":\"chensimin\",\n",
    "    \"stu10002\":\"hyuna\",\n",
    "    \"stu10003\":\"aom\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "88f1a831",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu10001': 'chensimin', 'stu10002': 'hyuna', 'stu10003': 'aom'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3875e253",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu10001 = {\n",
    "    \"name\":\"lxx\",\n",
    "    \"age\":19,\n",
    "    \"Job\":\"student\"\n",
    "}\n",
    "stu10002 = {\n",
    "    \"name\":\"hyuna\",\n",
    "    \"age\":26,\n",
    "    \"job\":\"singer and dancer\"    \n",
    "}\n",
    "stu10003 = {\n",
    "    \"name\":\"aom\",\n",
    "    \"age\":30,\n",
    "    \"job\":\"actor\"    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5ddf4fa8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu10001': 'chensimin', 'stu10002': 'hyuna', 'stu10003': 'aom'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "926b39f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info_02 = dict(\n",
    "   stu10001 =  \"chensimin\",\n",
    "   stu10002 =  \"hyuna\",\n",
    "   stu10003 =  \"aom\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "940b7683",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu10001': 'chensimin', 'stu10002': 'hyuna', 'stu10003': 'aom'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info_02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "254d850d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu10001': 'chensimin', 'stu10002': 'hyuna', 'stu10003': 'aom'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第三中创建字典的方式，新建空字典，然后增加值的方式创建字典\n",
    "\n",
    "stu_info_03 = {}\n",
    "\n",
    "stu_info_03['stu10001'] = \"chensimin\" # 字典的增加\n",
    "stu_info_03['stu10002'] = \"hyuna\" \n",
    "stu_info_03['stu10003'] = \"aom\" \n",
    "stu_info_03"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e4e3be94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu10001': 'chensimin', 'stu10002': 'hyuna', 'stu10003': 'aom'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info_03"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae1e246a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
